﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Xml;
using System.Security.Cryptography;
using System.Net;
using System.Text;
using WuYuHotel.SQLConn;
using System.Threading;

namespace WuYuHotel.Tools
{
    public class ZhuZheAPI
    {
        BaseDao basedao = new BaseDao();

        public void SynZhuZheAPI_Thread()
        {

            XmlDocument xml = new XmlDocument();

            while (true)
            {
                //获取所有房型信息 209412  210883
                //登录账号：wuyuHotel 密码：j5b225  

                //xml = FuncAction("zhuzher.room.getMemberRoomPrice", XmlInput_getMemberRoomPrice("210901", "", DateTime.Now, DateTime.Now.AddDays(65)));
                //if (XMLAnalysis(xml, "210901"))
                //{
                //    Thread.Sleep(150000);
                //}
                //else
                //{
                //    Dictionary<string, object> datalog = new Dictionary<string, object>();
                //    datalog.Add("type", int.Parse("2"));
                //    datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                //    datalog.Add("OpLog", "异常，线程SynZhuZheAPI_Thread访问getMemberRoomPrice,hotelId=210883返回错误值");
                //    basedao.save("T_LOG", datalog);
                //    Thread.Sleep(30000);
                //}

                 
                //210888
                xml = FuncAction("zhuzher.room.getMemberRoomPrice", XmlInput_getMemberRoomPrice("210888", "", DateTime.Now, DateTime.Now.AddDays(30)));
                if (XMLAnalysis(xml, "210888"))
                {
                    Thread.Sleep(1500);
                }
                else
                {
                    Dictionary<string, object> datalog = new Dictionary<string, object>();
                    datalog.Add("type", int.Parse("2"));
                    datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    datalog.Add("OpLog", "异常，线程SynZhuZheAPI_Thread访问getMemberRoomPrice,hotelId=210883返回错误值");
                    basedao.save("T_LOG", datalog);
                    Thread.Sleep(30000);
                }
                xml = FuncAction("zhuzher.room.getMemberRoomPrice", XmlInput_getMemberRoomPrice("210888", "", DateTime.Now.AddDays(31), DateTime.Now.AddDays(65)));
                if (XMLAnalysis(xml, "210888"))
                {
                    Thread.Sleep(150000);
                }
                else
                {
                    Dictionary<string, object> datalog = new Dictionary<string, object>();
                    datalog.Add("type", int.Parse("2"));
                    datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    datalog.Add("OpLog", "异常，线程SynZhuZheAPI_Thread访问getMemberRoomPrice,hotelId=210883返回错误值");
                    basedao.save("T_LOG", datalog);
                    Thread.Sleep(30000);
                }

                //209412
                xml = FuncAction("zhuzher.room.getMemberRoomPrice", XmlInput_getMemberRoomPrice("209412","",DateTime.Now,DateTime.Now.AddDays(30)));
                if(XMLAnalysis(xml, "209412"))
                {
                    Thread.Sleep(1500);
                }
                else
                {
                    Dictionary<string, object> datalog = new Dictionary<string, object>();
                    datalog.Add("type", int.Parse("2"));
                    datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    datalog.Add("OpLog", "异常，线程SynZhuZheAPI_Thread访问getMemberRoomPrice,hotelId=209412返回错误值");
                    basedao.save("T_LOG", datalog);
                    Thread.Sleep(30000);
                }
                xml = FuncAction("zhuzher.room.getMemberRoomPrice", XmlInput_getMemberRoomPrice("209412", "", DateTime.Now.AddDays(31), DateTime.Now.AddDays(65)));
                if (XMLAnalysis(xml, "209412"))
                {
                    Thread.Sleep(150000);
                }
                else
                {
                    Dictionary<string, object> datalog = new Dictionary<string, object>();
                    datalog.Add("type", int.Parse("2"));
                    datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    datalog.Add("OpLog", "异常，线程SynZhuZheAPI_Thread访问getMemberRoomPrice,hotelId=209412返回错误值");
                    basedao.save("T_LOG", datalog);
                    Thread.Sleep(30000);
                }


                //210883
                xml = FuncAction("zhuzher.room.getMemberRoomPrice", XmlInput_getMemberRoomPrice("210883", "", DateTime.Now, DateTime.Now.AddDays(30)));
                if (XMLAnalysis(xml, "210883"))
                {
                    Thread.Sleep(150000);
                }
                else
                {
                    Dictionary<string, object> datalog = new Dictionary<string, object>();
                    datalog.Add("type", int.Parse("2"));
                    datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    datalog.Add("OpLog", "异常，线程SynZhuZheAPI_Thread访问getMemberRoomPrice,hotelId=210883返回错误值");
                    basedao.save("T_LOG", datalog);
                    Thread.Sleep(30000);
                }
                xml = FuncAction("zhuzher.room.getMemberRoomPrice", XmlInput_getMemberRoomPrice("210883", "", DateTime.Now.AddDays(31), DateTime.Now.AddDays(65)));
                if (XMLAnalysis(xml, "210883"))
                {
                    Thread.Sleep(150000);
                }
                else
                {
                    Dictionary<string, object> datalog = new Dictionary<string, object>();
                    datalog.Add("type", int.Parse("2"));
                    datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    datalog.Add("OpLog", "异常，线程SynZhuZheAPI_Thread访问getMemberRoomPrice,hotelId=210883返回错误值");
                    basedao.save("T_LOG", datalog);
                    Thread.Sleep(30000);
                }


            }
        }

        public bool XMLAnalysis(XmlDocument xml,string HotelId)
        {
            int i, j, loopNum, loopdays;
            string sql = "";
            string sqlTempTable = "";
            if (xml != null && xml.SelectSingleNode("//result/errorMsg").InnerText.Contains("调用成功"))
            {
                //
                loopdays = xml.SelectSingleNode("//days").ChildNodes.Count;
                loopNum = xml.SelectSingleNode("//roomTypes").ChildNodes.Count;

                for (i = 0; i < loopdays; i++)
                {
                    string roomdate = xml.SelectSingleNode("//days").ChildNodes[i]["date"].InnerText;
                    loopNum = xml.SelectSingleNode("//days").ChildNodes[i]["roomTypes"].ChildNodes.Count;
                    for (j = 0; j < loopNum; j++)
                    {

                        string roomtypename = xml.SelectSingleNode("//days").ChildNodes[i].SelectSingleNode("roomTypes").ChildNodes[j]["roomTypeName"].InnerText;
                        string hotelId = HotelId;
                        string roomTypeId = xml.SelectSingleNode("//days").ChildNodes[i].SelectSingleNode("roomTypes").ChildNodes[j]["roomTypeId"].InnerText;
                        string available = xml.SelectSingleNode("//days").ChildNodes[i].SelectSingleNode("roomTypes").ChildNodes[j]["available"].InnerText;
                        string fitPrice = xml.SelectSingleNode("//days").ChildNodes[i].SelectSingleNode("roomTypes").ChildNodes[j]["fitPrice"].InnerText;

                        if (i == 0 && j == 0)
                        {
                            sqlTempTable += " select '" + roomtypename + "' as roomTypeName,'" + hotelId + "' as hotelId," + roomTypeId + " as roomTypeId," + available + " as available," + fitPrice + " as price,'" + roomdate + "' as roomdate";

                        }
                        else
                        {
                            sqlTempTable += " UNION ALL select '" + roomtypename + "' as roomTypeName,'" + hotelId + "' as hotelId," + roomTypeId + " as roomTypeId," + available + " as available," + fitPrice + " as price,'" + roomdate + "' as roomdate";
                        }

                    }
                }

                sql = "BEGIN select * into #TempPrice from (" + sqlTempTable + ") as a;";
                sql += " MERGE INTO t_m_roomType as t ";
                sql += " USING #TempPrice AS s ON t.hotelId=s.hotelId AND t.roomTypeId=s.roomTypeId AND t.roomdate=s.roomdate";
                sql += " WHEN MATCHED THEN UPDATE SET t.available=s.available,t.price=s.price,t.roomTypeName=s.roomTypeName";
                sql += " WHEN NOT MATCHED BY TARGET THEN INSERT(roomTypeId,hotelId,roomTypeName,price,available,roomdate) VALUES(s.roomTypeId,s.hotelId,s.roomTypeName,s.price,s.available,s.roomdate);";
                sql += " DROP TABLE #TempPrice;";
                sql += " END";

                try
                {
                    BaseDao.execute(sql);

                }
                catch (Exception ex)
                {
                    Dictionary<string, object> datalog = new Dictionary<string, object>();
                    datalog.Add("type", int.Parse("2"));
                    datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    datalog.Add("OpLog", "异常，线程SynZhuZheAPI_Thread第1个try:" + ex.Message.ToString().Replace("'", "''").Replace(" ", ""));
                    basedao.save("T_LOG", datalog);
                }

                //Sleep
                return true;
            }
            else
            {
                //30秒过后继续去查
                return false;
            }
        }


        /// <summary>
        /// 获取房型和可定数量
        /// </summary>
        /// <param name="HotelId">酒店ID</param>
        /// <param name="RoomTypeId">房型ID</param>
        /// <param name="enter">开始时间</param>
        /// <param name="leave">结束时间</param>
        /// <returns></returns>
        public string XmlInput_getMemberRoomPrice(string HotelId,string RoomTypeId,DateTime enter,DateTime leave)
        {
            XmlDocument XmlDoc = new XmlDocument();

            //加入根节点 request
            XmlElement rootElement = XmlDoc.CreateElement("request");
            XmlDoc.AppendChild(rootElement);

            XmlElement headElement = XmlDoc.CreateElement("head");
            headElement.InnerText = "";
            rootElement.AppendChild(headElement);

            XmlElement bodyElement = XmlDoc.CreateElement("body");

            XmlElement hotelIdElement = XmlDoc.CreateElement("hotelId");
            hotelIdElement.InnerText = HotelId;
            bodyElement.AppendChild(hotelIdElement);

            XmlElement roomTypeID = XmlDoc.CreateElement("roomTypeId");
            roomTypeID.InnerText = RoomTypeId;
            bodyElement.AppendChild(roomTypeID);

            XmlElement startDate = XmlDoc.CreateElement("startDate");
            startDate.InnerText = enter.ToString("yyyy-MM-dd");
            bodyElement.AppendChild(startDate);

            XmlElement endtDate = XmlDoc.CreateElement("endDate");
            endtDate.InnerText = leave.ToString("yyyy-MM-dd");
            bodyElement.AppendChild(endtDate);

            XmlElement comefrom = XmlDoc.CreateElement("comefrom");
            comefrom.InnerText = "21";
            bodyElement.AppendChild(comefrom);

            rootElement.AppendChild(bodyElement);

            return XmlDoc.OuterXml;

        }

        public string RegisterMember(string UserName,string Tel)
        {
            //zhuzher.member.registerMember

            XmlDocument XmlDoc = new XmlDocument();

            XmlElement rootElement = XmlDoc.CreateElement("request");
            XmlDoc.AppendChild(rootElement);

            XmlElement headElement = XmlDoc.CreateElement("head");
            headElement.InnerText = "";
            rootElement.AppendChild(headElement);

            XmlElement bodyElement = XmlDoc.CreateElement("body");

            XmlElement hotelIdElement = XmlDoc.CreateElement("hotelId");
            hotelIdElement.InnerText = "210883";
            bodyElement.AppendChild(hotelIdElement);

            XmlElement password = XmlDoc.CreateElement("password");
            password.InnerText = "123456";
            bodyElement.AppendChild(password);

            XmlElement name = XmlDoc.CreateElement("name");
            name.InnerText = UserName;
            bodyElement.AppendChild(name);

            XmlElement tel = XmlDoc.CreateElement("tel");
            tel.InnerText = Tel;
            bodyElement.AppendChild(tel);

            XmlElement email = XmlDoc.CreateElement("email");
            email.InnerText = "";
            bodyElement.AppendChild(email);

            XmlElement sex = XmlDoc.CreateElement("sex");
            sex.InnerText = "";
            bodyElement.AppendChild(sex);

            XmlElement birthday = XmlDoc.CreateElement("birthday");
            birthday.InnerText = "";
            bodyElement.AppendChild(birthday);

            XmlElement address = XmlDoc.CreateElement("address");
            address.InnerText = "";
            bodyElement.AppendChild(address);

            XmlElement cardNo = XmlDoc.CreateElement("cardNo");
            cardNo.InnerText = "";
            bodyElement.AppendChild(cardNo);

            XmlElement cardType = XmlDoc.CreateElement("cardType");
            cardType.InnerText = "";
            bodyElement.AppendChild(cardType);

            XmlElement province = XmlDoc.CreateElement("province");
            province.InnerText = "";
            bodyElement.AppendChild(province);

            XmlElement interest = XmlDoc.CreateElement("interest");
            interest.InnerText = "";
            bodyElement.AppendChild(interest);

            XmlElement level = XmlDoc.CreateElement("level");
            level.InnerText = "";
            bodyElement.AppendChild(level);

            XmlElement levelName = XmlDoc.CreateElement("levelName");
            levelName.InnerText = "";
            bodyElement.AppendChild(levelName);


            rootElement.AppendChild(bodyElement);

            return XmlDoc.OuterXml;
        }

        /// <summary>
        /// 任意查询一个分店的会员即可
        /// </summary>
        /// <param name="Tel">查询的手机号</param>
        /// <returns></returns>
        public string GetMemberNo(string Tel)
        {
            //zhuzher.member.getMemberNoByInfo

            XmlDocument XmlDoc = new XmlDocument();

            XmlElement rootElement = XmlDoc.CreateElement("request");
            XmlDoc.AppendChild(rootElement);

            XmlElement headElement = XmlDoc.CreateElement("head");
            headElement.InnerText = "";
            rootElement.AppendChild(headElement);

            XmlElement bodyElement = XmlDoc.CreateElement("body");

            XmlElement hotelIdElement = XmlDoc.CreateElement("hotelId");
            hotelIdElement.InnerText = "209412";
            bodyElement.AppendChild(hotelIdElement);

            XmlElement idCardNo = XmlDoc.CreateElement("idCardNo");
            idCardNo.InnerText = "";
            bodyElement.AppendChild(idCardNo);

            XmlElement mail = XmlDoc.CreateElement("mail");
            mail.InnerText = "";
            bodyElement.AppendChild(mail);

            XmlElement phoneNo = XmlDoc.CreateElement("phoneNo");
            phoneNo.InnerText = Tel;
            bodyElement.AppendChild(phoneNo);



            rootElement.AppendChild(bodyElement);

            return XmlDoc.OuterXml;


        }

        public string XmlInput_GetHotelInfo()
        {
            XmlDocument XmlDoc = new XmlDocument();

            //加入根节点 request
            XmlElement rootElement = XmlDoc.CreateElement("request");
            XmlDoc.AppendChild(rootElement);

            XmlElement headElement = XmlDoc.CreateElement("head");
            headElement.InnerText = "";
            rootElement.AppendChild(headElement);

            XmlElement bodyElement = XmlDoc.CreateElement("body");
            XmlElement hotelIdElement = XmlDoc.CreateElement("hotelId");
            hotelIdElement.InnerText = "209412";
            bodyElement.AppendChild(hotelIdElement);

            rootElement.AppendChild(bodyElement);

            return XmlDoc.OuterXml;


        }

        public string PayOrder(string p_hotelId,string p_orderId,string p_tradeId,string p_money)
        {
            //zhuzher.order.payOrderInfo
            XmlDocument XmlDoc = new XmlDocument();

            //加入根节点 request
            XmlElement rootElement = XmlDoc.CreateElement("request");
            XmlDoc.AppendChild(rootElement);

            XmlElement headElement = XmlDoc.CreateElement("head");
            headElement.InnerText = "";
            rootElement.AppendChild(headElement);

            XmlElement bodyElement = XmlDoc.CreateElement("body");

            XmlElement hotelId = XmlDoc.CreateElement("hotelId");
            hotelId.InnerText = p_hotelId;
            bodyElement.AppendChild(hotelId);

            XmlElement orderId = XmlDoc.CreateElement("orderId");
            orderId.InnerText = p_orderId;
            bodyElement.AppendChild(orderId);

            XmlElement tradeId = XmlDoc.CreateElement("tradeId");
            tradeId.InnerText = p_tradeId;
            bodyElement.AppendChild(tradeId);

            XmlElement agent = XmlDoc.CreateElement("agent");
            agent.InnerText = "微信公众号支付";
            bodyElement.AppendChild(agent);

            XmlElement projectId = XmlDoc.CreateElement("projectId");
            projectId.InnerText = "";
            bodyElement.AppendChild(projectId);

            XmlElement money = XmlDoc.CreateElement("money");
            money.InnerText = p_money;
            bodyElement.AppendChild(money);

            XmlElement ifGuarantor = XmlDoc.CreateElement("ifGuarantor");
            ifGuarantor.InnerText = "0";
            bodyElement.AppendChild(ifGuarantor);

            rootElement.AppendChild(bodyElement);

            return XmlDoc.OuterXml;
        }


        public  string SubmitOrder2(string P_HotelID, string P_MemberNo, int RoomNum, DateTime enter, DateTime leave, string P_RoomTypeId,string P_RoomName, string P_totalPrice, string PersonName, string tel, string Remark)
        {
            XmlDocument XmlDoc = new XmlDocument();

            XmlElement rootElement = XmlDoc.CreateElement("request");
            XmlDoc.AppendChild(rootElement);

            XmlElement headElement = XmlDoc.CreateElement("head");
            headElement.InnerText = "";
            rootElement.AppendChild(headElement);

            XmlElement bodyElement = XmlDoc.CreateElement("body");

            XmlElement hotelIdElement = XmlDoc.CreateElement("hotelId");
            hotelIdElement.InnerText = P_HotelID;
            bodyElement.AppendChild(hotelIdElement);

            XmlElement roomTypeId = XmlDoc.CreateElement("roomTypeId");
            roomTypeId.InnerText = P_RoomTypeId;      //测试房间ID
            bodyElement.AppendChild(roomTypeId);

            XmlElement roomTypeName = XmlDoc.CreateElement("roomTypeName");
            roomTypeName.InnerText = P_RoomName;
            bodyElement.AppendChild(roomTypeName);

            XmlElement startDate = XmlDoc.CreateElement("startTime");
            startDate.InnerText = enter.ToString("yyyy-MM-dd 12:00:00");
            bodyElement.AppendChild(startDate);

            XmlElement endStart = XmlDoc.CreateElement("endTime");
            endStart.InnerText = leave.ToString("yyyy-MM-dd 12:00:00");
            bodyElement.AppendChild(endStart);

            XmlElement roomNum = XmlDoc.CreateElement("roomNum");
            roomNum.InnerText = RoomNum.ToString();
            bodyElement.AppendChild(roomNum);

            XmlElement ratePlanId = XmlDoc.CreateElement("ratePlanId");
            ratePlanId.InnerText = "";
            bodyElement.AppendChild(ratePlanId);

            XmlElement bookNum = XmlDoc.CreateElement("bookNum");
            bookNum.InnerText = P_MemberNo;
            bodyElement.AppendChild(bookNum);


            XmlElement checkInPerson = XmlDoc.CreateElement("checkInPerson");
            checkInPerson.InnerText = PersonName;
            bodyElement.AppendChild(checkInPerson);

            XmlElement checkInTel = XmlDoc.CreateElement("checkInTel");
            checkInTel.InnerText = tel;
            bodyElement.AppendChild(checkInTel);

            XmlElement cardNo = XmlDoc.CreateElement("cardNo");
            cardNo.InnerText = "";
            bodyElement.AppendChild(cardNo);

            XmlElement comefrom = XmlDoc.CreateElement("comefrom");
            comefrom.InnerText = "21";
            bodyElement.AppendChild(comefrom);

            XmlElement keepTime = XmlDoc.CreateElement("keepTime");
            keepTime.InnerText = "23:59";
            bodyElement.AppendChild(keepTime);

            XmlElement totalPrice = XmlDoc.CreateElement("totalPrice");
            totalPrice.InnerText = P_totalPrice;
            bodyElement.AppendChild(totalPrice);

            XmlElement ensureFlag = XmlDoc.CreateElement("ensureFlag");
            ensureFlag.InnerText = "0";     //表示已支付定金
            bodyElement.AppendChild(ensureFlag);

            XmlElement guarantorType = XmlDoc.CreateElement("guarantorType");
            guarantorType.InnerText = "无担保";
            bodyElement.AppendChild(guarantorType);

            XmlElement remark = XmlDoc.CreateElement("remark");
            remark.InnerText = Remark;
            bodyElement.AppendChild(remark);

            //XmlElement xxx = XmlDoc.CreateElement("xxx");
            //xxx.InnerText = "xxx";
            //orderItem.AppendChild(xxx);

            rootElement.AppendChild(bodyElement);

            return XmlDoc.OuterXml;

        }

        public string SubmitOrder(string P_HotelID, string P_MemberNo, int RoomNum, DateTime enter, DateTime leave, string P_RoomTypeId, string P_totalPrice, string PersonName, string tel,string Remark)
        {
            //zhuzher.member.submitOrder

            XmlDocument XmlDoc = new XmlDocument();

            XmlElement rootElement = XmlDoc.CreateElement("request");
            XmlDoc.AppendChild(rootElement);

            XmlElement headElement = XmlDoc.CreateElement("head");
            headElement.InnerText = "";
            rootElement.AppendChild(headElement);

            XmlElement bodyElement = XmlDoc.CreateElement("body");

            XmlElement hotelIdElement = XmlDoc.CreateElement("hotelId");
            hotelIdElement.InnerText = P_HotelID;
            bodyElement.AppendChild(hotelIdElement);

            XmlElement memberNo = XmlDoc.CreateElement("memberNo");
            memberNo.InnerText = P_MemberNo;
            bodyElement.AppendChild(memberNo);

            XmlElement ensureFlag = XmlDoc.CreateElement("ensureFlag");
            ensureFlag.InnerText = "0";     //表示已支付定金
            bodyElement.AppendChild(ensureFlag);

            XmlElement guarantorType = XmlDoc.CreateElement("guarantorType");
            guarantorType.InnerText = "0";
            bodyElement.AppendChild(guarantorType);

            XmlElement remark = XmlDoc.CreateElement("remark");
            remark.InnerText = Remark;
            bodyElement.AppendChild(remark);

            XmlElement bookNum = XmlDoc.CreateElement("bookNum");
            bookNum.InnerText = "";
            bodyElement.AppendChild(bookNum);

            XmlElement keepTime = XmlDoc.CreateElement("keepTime");
            keepTime.InnerText = "18:00";
            bodyElement.AppendChild(keepTime);

            XmlElement totalPrice = XmlDoc.CreateElement("totalPrice");
            totalPrice.InnerText = P_totalPrice;
            bodyElement.AppendChild(totalPrice);

            for (int i = 0; i < RoomNum; i++)
            {

                XmlElement orderItem = XmlDoc.CreateElement("orderItem");
                orderItem.InnerText = "";
                bodyElement.AppendChild(orderItem);

                XmlElement startDate = XmlDoc.CreateElement("startDate");
                startDate.InnerText = enter.ToString("yyyy-MM-dd 12:00:00");
                orderItem.AppendChild(startDate);

                XmlElement endStart = XmlDoc.CreateElement("endStart");
                endStart.InnerText = leave.ToString("yyyy-MM-dd 12:00:00");
                orderItem.AppendChild(endStart);

                XmlElement roomTypeId = XmlDoc.CreateElement("roomTypeId");
                roomTypeId.InnerText = P_RoomTypeId;      //测试房间ID
                orderItem.AppendChild(roomTypeId);

                XmlElement roomTypeName = XmlDoc.CreateElement("roomTypeName");
                roomTypeName.InnerText = "";
                orderItem.AppendChild(roomTypeName);

                XmlElement checkInPerson = XmlDoc.CreateElement("checkInPerson");
                checkInPerson.InnerText = PersonName;
                orderItem.AppendChild(checkInPerson);

                XmlElement checkInTel = XmlDoc.CreateElement("checkInTel");
                checkInTel.InnerText = tel;
                orderItem.AppendChild(checkInTel);

                XmlElement cardNo = XmlDoc.CreateElement("cardNo");
                cardNo.InnerText = "";
                orderItem.AppendChild(cardNo);

                XmlElement ratePlanId = XmlDoc.CreateElement("ratePlanId");
                ratePlanId.InnerText = "";
                orderItem.AppendChild(ratePlanId);

                //bodyElement.AppendChild(orderItem);


            }
            //XmlElement xxx = XmlDoc.CreateElement("xxx");
            //xxx.InnerText = "xxx";
            //orderItem.AppendChild(xxx);

            rootElement.AppendChild(bodyElement);

            return XmlDoc.OuterXml;

        }

        public string CancelOrder(string HotelId,string ZZOrderId)
        {
            XmlDocument XmlDoc = new XmlDocument();

            //加入根节点 request
            XmlElement rootElement = XmlDoc.CreateElement("request");
            XmlDoc.AppendChild(rootElement);

            XmlElement headElement = XmlDoc.CreateElement("head");
            headElement.InnerText = "";
            rootElement.AppendChild(headElement);

            XmlElement bodyElement = XmlDoc.CreateElement("body");

            XmlElement hotelIdElement = XmlDoc.CreateElement("hotelId");
            hotelIdElement.InnerText = HotelId;
            bodyElement.AppendChild(hotelIdElement);

            XmlElement orderId = XmlDoc.CreateElement("orderId");
            orderId.InnerText = ZZOrderId;
            bodyElement.AppendChild(orderId);

            rootElement.AppendChild(bodyElement);

            return XmlDoc.OuterXml;


        }

        public XmlDocument FuncAction(string method,string inputObj)
        {
            string url = "http://open.zhuzher.com/api/request.action"; //
            //string method = "zhuzher.data.getAllRoomTypeInfo";
            string cId = "C0935201";
            string hotelId = "209412";
            long time = new DateTime().currentTimeMillis();
            string key = "123456";
            string dataKey = "12345678";
            string authCode = funcMD5(cId + method + time + key);
            //string XML_GetHotelInfo = GetHotelInfo();

            //string inputObj = XML_GetHotelInfo;
            inputObj = desCrypto(inputObj, dataKey);
            inputObj = Uri.EscapeDataString(inputObj);
            //下面拼好请求字符串
            string queryString = "cId=" + cId + "&authCode=" + authCode + "&method=" + method + "&time=" + time + "&data=" + inputObj;

            byte[] PostData = Encoding.UTF8.GetBytes(queryString);
            WebClient webClient = new WebClient();
            webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            try
            {
                byte[] ResponseData = webClient.UploadData(url, "POST", PostData);
                string SrcString = Encoding.UTF8.GetString(ResponseData);
                //Console.WriteLine(SrcString);

                XmlDocument xml = new XmlDocument();
                xml.LoadXml(SrcString);
                return xml;
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("2"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", "0");
                datalog.Add("OpLog", "异常，获取令牌时候抛出的catch :" + ex.Message.ToString().Replace("'", "''").Replace(" ", ""));
                basedao.save("T_LOG", datalog);
                return null;

                
            }

            // xml.SelectSingleNode("")
        }


        public string funcMD5(string sDataIn)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bytValue, bytHash;
            bytValue = System.Text.Encoding.UTF8.GetBytes(sDataIn);
            bytHash = md5.ComputeHash(bytValue);
            md5.Clear();
            string sTemp = "";
            for (int i = 0; i < bytHash.Length; i++)
            {
                sTemp += bytHash[i].ToString("X").PadLeft(2, '0');
            }
            return sTemp.ToLower();
        }


        public string desCrypto(string datasource, string password)
        {
            DESCryptoServiceProvider objDESCryptoServiceProvider = new DESCryptoServiceProvider();
            objDESCryptoServiceProvider.Key = Encoding.UTF8.GetBytes(password);
            objDESCryptoServiceProvider.IV = Encoding.UTF8.GetBytes(password);
            objDESCryptoServiceProvider.Mode = CipherMode.ECB;
            objDESCryptoServiceProvider.Padding = PaddingMode.PKCS7;

            byte[] bysData = Encoding.UTF8.GetBytes(datasource);
            byte[] bysFixSizeData = new byte[(int)(Math.Ceiling(bysData.Length / 8.0) * 8)];
            Array.Copy(bysData, bysFixSizeData, bysData.Length);
            byte[] bysEncrypted = objDESCryptoServiceProvider.CreateEncryptor().TransformFinalBlock(bysFixSizeData, 0, bysFixSizeData.Length);
            return Convert.ToBase64String(bysEncrypted);
        }


    }
    /// <summary>
    /// DeteTime扩展方法，达到java里面System.currentTimeMillis()功能
    /// </summary>
    public static class DateTimeExtend
    {
        private static DateTime GMTIni = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        public static long currentTimeMillis(this DateTime d)
        {
            return (long)((DateTime.UtcNow - GMTIni).TotalMilliseconds);
        }
    }






}
